﻿using System;
using System.Collections.Generic;
using NoMiddleman.Core.Domain;

namespace NoMiddleman.Facades.DataTransferObjects
{
    public class AdvertDTO
    {
        public int Id { get; set; }

        public string Title { get; set; }

        public string Description { get; set; }

        public ClientDTO Client { get; set; }

        public DateTime DateAdded { get; set; }

        public IList<PhotoDTO> Photos { get; set; }

        public RealEstateDTO RealEstate { get; set; }

        public bool IsForSale { get; set; }

        public decimal Rent { get; set; }

        public decimal Price { get; set; }
    }

    public class AdvertFactory
    {
        public static Advert GetAdvert(AdvertDTO advertDTO)
        {
            if(advertDTO.IsForSale)
            {
                Sale entity = new Sale
                                  {
                                      Client = ClientFactory.GetClient(advertDTO.Client),
                                      DateAdded = advertDTO.DateAdded,
                                      Description = advertDTO.Description,
                                      RealEstate = RealEstateFactory.GetRealEstate(advertDTO.RealEstate),
                                      Title = advertDTO.Title,
                                      Price = advertDTO.Price
                                      
                                  };
                return entity;
            }
            else
            {
                Tenancy entity = new Tenancy
                                     {
                                         Client = ClientFactory.GetClient(advertDTO.Client),
                                         DateAdded = advertDTO.DateAdded,
                                         Description = advertDTO.Description,
                                         RealEstate = RealEstateFactory.GetRealEstate(advertDTO.RealEstate),
                                         Title = advertDTO.Title,
                                         Rent = advertDTO.Rent
                                     };
                return entity;
            }
        }
        public static AdvertDTO GetAdvertDTO(Advert advert)
        {
            AdvertDTO entity = new AdvertDTO();
            entity.Client = ClientFactory.GetClientDTO(advert.Client);
            entity.DateAdded = advert.DateAdded;
            entity.Description = advert.Description;
            entity.Id = advert.Id;
            entity.IsForSale = true;
            entity.RealEstate = RealEstateFactory.GetRealEstateDTO(advert.RealEstate);
            entity.Title = advert.Title;

            Sale s = advert as Sale;
            if(s!=null)
            {
                entity.Price = s.Price;
            }
            else
            {
                entity.Rent = ((Tenancy) advert).Rent;
            }
            return entity;
        }
    }
}